package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseEquipment;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBaseEquipmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备基础Controller
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@Api(tags = "设备基础api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/equipment" )
public class BaseEquipmentController extends BaseController {

    private final IBaseEquipmentService iBaseEquipmentService;

    /**
     * 查询设备基础列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseEquipment baseEquipment)
    {
        startPage();
        LambdaQueryWrapper<BaseEquipment> lqw = new LambdaQueryWrapper<BaseEquipment>();
        if (StringUtils.isNotBlank(baseEquipment.getEquipmentName())){
            lqw.like(BaseEquipment::getEquipmentName ,baseEquipment.getEquipmentName());
        }
        if (StringUtils.isNotBlank(baseEquipment.getEquipmentCode())){
            lqw.eq(BaseEquipment::getEquipmentCode ,baseEquipment.getEquipmentCode());
        }
        if (StringUtils.isNotBlank(baseEquipment.getEquipmentType())){
            lqw.eq(BaseEquipment::getEquipmentType ,baseEquipment.getEquipmentType());
        }
        if (baseEquipment.getShopId() != null){
            lqw.eq(BaseEquipment::getShopId ,baseEquipment.getShopId());
        }
        List<BaseEquipment> list = iBaseEquipmentService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出设备基础列表
     */
    @Log(title = "设备基础" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BaseEquipment baseEquipment) {
        LambdaQueryWrapper<BaseEquipment> lqw = new LambdaQueryWrapper<BaseEquipment>(baseEquipment);
        List<BaseEquipment> list = iBaseEquipmentService.list(lqw);
        ExcelUtil<BaseEquipment> util = new ExcelUtil<BaseEquipment>(BaseEquipment. class);
        return util.exportExcel(list, "equipment" );
    }

    /**
     * 获取设备基础详细信息
     */
    @GetMapping(value = "/{equipmentId}" )
    public AjaxResult getInfo(@PathVariable("equipmentId" ) Long equipmentId) {
        return AjaxResult.success(iBaseEquipmentService.getById(equipmentId));
    }

    /**
     * 新增设备基础
     */
    @Log(title = "设备基础" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseEquipment baseEquipment) {
        return toAjax(iBaseEquipmentService.save(baseEquipment) ? 1 : 0);
    }

    /**
     * 修改设备基础
     */
    @Log(title = "设备基础" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseEquipment baseEquipment) {
        return toAjax(iBaseEquipmentService.updateById(baseEquipment) ? 1 : 0);
    }

    /**
     * 删除设备基础
     */
    @Log(title = "设备基础" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{equipmentIds}" )
    public AjaxResult remove(@PathVariable Long[] equipmentIds) {
        return toAjax(iBaseEquipmentService.removeByIds(Arrays.asList(equipmentIds)) ? 1 : 0);
    }
}
